package org.mspring.mlog.security.service;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.dao.UserDao;
import org.mspring.mlog.entity.User;
import org.mspring.nbee.common.utils.PropertyConfigurer;
import org.mspring.nbee.orm.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.orm.dao.sql.pager.PageResult;
import org.mspring.nbee.orm.dao.sql.pager.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2014-09-10 12:44:49
 */
@Service
public class UserService {

    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);

    /**
     * 默认密码加密盐值
     */
    public static final String DEFAULT_SALT = "_mlog_pwd_salt_";

    private UserDao userDao;
    private PropertyConfigurer propertyConfigurer;

    public User findById(long id) {
        return userDao.findById(id);
    }

    public User findByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return userDao.findByUsername(username);
    }

    public User login(String username, String password) {
        if (StringUtils.isAnyBlank(username, password)) {
            return null;
        }
        User user = findByUsername(username);
        if (user == null) {
            return null;
        }
        password = genereatePassword(password);
        if (StringUtils.equals(user.getPassword(), password)) {
            return user;
        }
        return null;
    }

    /**
     * 生成密码
     *
     * @param password
     * @return
     */
    public String genereatePassword(String password) {
        String salt = propertyConfigurer.getString("salt", DEFAULT_SALT);
        byte[] input = password.getBytes(Charset.forName("UTF-8"));
        MessageDigest digest = DigestUtils.getMd5Digest();
        if (StringUtils.isNotBlank(salt)) {
            digest.update(salt.getBytes());
        }
        byte[] result = digest.digest(input);
        for (int i = 1; i < 64; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return Hex.encodeHexString(result);
    }

    public PageResult<User> list(List<SqlCondition> conditions, Pager pager) {
        return userDao.listPage(conditions, pager);
    }

    @Transactional
    public boolean update(User user) {
        return userDao.update(user);
    }

    @Transactional
    public boolean deleteById(long id) {
        return userDao.deleteById(id);
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Autowired
    public void setPropertyConfigurer(PropertyConfigurer propertyConfigurer) {
        this.propertyConfigurer = propertyConfigurer;
    }
}
